package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.OmsOrderReturnReason;
import com.macro.mall.portal.domain.OmsOrderReturnApplyDto;
import com.macro.mall.portal.domain.OmsOrderReturnApplyParam;
import com.macro.mall.portal.service.OmsPortalOrderReturnApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 申请退货管理Controller
 */
@Controller
@Api(tags = "OmsPortalOrderReturnApplyController", description = "申请退货管理")
@RequestMapping("/returnApply")
public class OmsPortalOrderReturnApplyController {
    @Autowired
    private OmsPortalOrderReturnApplyService returnApplyService;

    @ApiOperation("申请退货")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody OmsOrderReturnApplyParam returnApplyParam) {
        int flag = returnApplyService.create(returnApplyParam);
        if (flag > 0) {
            return CommonResult.success(flag);
        }else if (flag == -1){
            return CommonResult.failed("确认收货超过规定时限，不能申请售后");
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询售后订单")
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<OmsOrderReturnApplyDto>> getList(@RequestParam(required = false) String productName,
                                                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return returnApplyService.getList(productName, pageSize, pageNum);
    }


    @ApiOperation("根据子订单id查询售后单详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<OmsOrderReturnApplyDto> getReturnList(@RequestParam Long id) {
        OmsOrderReturnApplyDto orderApply = returnApplyService.getReturnList(id);
        return CommonResult.success(orderApply);
    }

    @ApiOperation("申请换货")
    @RequestMapping(value = "/exchange", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult exchange(@RequestBody OmsOrderReturnApplyParam orderReturnApplyParam) {
        return returnApplyService.exchange(orderReturnApplyParam);
    }

    @ApiOperation("查询换货商品的其他sku信息")
    @RequestMapping(value = "/getReturnSku", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getReturnSku(@RequestParam Long orderItemId) {
        CommonResult returnSku = returnApplyService.getReturnSku(orderItemId);
        return returnSku;
    }

    /**
     * 撤销售后订单
     * @param id
     * @return
     */
    @RequestMapping(value = "/repeal", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult repealReturnApply(@RequestParam Long id){
        return returnApplyService.repealReturnApply(id);
    }

    /**
     * 获取退货原因
     * @return
     */
    @RequestMapping(value = "/getReturnReason", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<OmsOrderReturnReason>> getOmsReturnReason(){
        return returnApplyService.getOmsReturnReason();
    }
}
